package com.woniuxy.school.online.comment.controller;


import com.woniuxy.commons.core.model.Result;
import com.woniuxy.commons.core.util.JwtUtil;
import com.woniuxy.school.online.comment.entity.RepairNotice;
import com.woniuxy.school.online.comment.from.AddNoticeForm;
import com.woniuxy.school.online.comment.from.DelectNoticeForm;
import com.woniuxy.school.online.comment.from.SelectNoticeByIdForm;
import com.woniuxy.school.online.comment.from.UpdateNoticeForm;
import com.woniuxy.school.online.comment.service.RepairNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 通知公告 前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2025年05月14日
 */
@RestController
@RequestMapping("/repairNotice")
@Api(tags = "通知公告模块")
public class RepairNoticeController {
    @Autowired
    private RepairNoticeService repairNoticeService;

    @GetMapping("/addNotice")
    @ApiOperation("添加公告")
    public Result addNotice(AddNoticeForm addNoticeForm, @RequestHeader String token) throws Exception {
        String userId = JwtUtil.parseUserToken(token).get("userId").toString();
        repairNoticeService.addNotice(addNoticeForm,userId);
        return Result.getResult().setMessage("添加公告成功");
    }
    @PostMapping("/deleteNotice")
    @ApiOperation("删除公告")
    public Result deleteNotice(@RequestBody DelectNoticeForm delectNoticeForm, @RequestHeader String token) throws Exception {
        String userId = JwtUtil.parseUserToken(token).get("userId").toString();
        repairNoticeService.deleteNotice(delectNoticeForm,userId);
        return Result.getResult().setMessage("添加公告成功");
    }
    @PostMapping("/updateNotice")
    @ApiOperation("修改公告")
    public Result updateNotice(@RequestBody UpdateNoticeForm updateNoticeForm, @RequestHeader String token) throws Exception {
        String userId = JwtUtil.parseUserToken(token).get("userId").toString();
        repairNoticeService.updateNotice(updateNoticeForm,userId);
        return Result.getResult().setMessage("修改公告成功");
    }
    @PostMapping("/selectNotice")
    @ApiOperation("查询公告")
    public Result<List<RepairNotice>> selectNotice() throws Exception {
        //查询所有公告
        List<RepairNotice> list = repairNoticeService.selectNotice();
        return Result.getListResult(RepairNotice.class).setMessage("查询公告成功").setData(list);
    }
    @PostMapping("/selectNoticeById")
    @ApiOperation("根据id查询公告详情")
    public Result<RepairNotice> selectNoticeById(@RequestBody SelectNoticeByIdForm selectNoticeByIdForm) throws Exception {
        RepairNotice repairNotice = repairNoticeService.getById(selectNoticeByIdForm.getId());
        return Result.getResult(RepairNotice.class).setMessage("查询公告成功").setData(repairNotice);
    }







}

